<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "tb_adminlog".
 *
 * @property string $log_id
 * @property string $title
 * @property int $addtime
 * @property string $admin_name
 * @property int $admin_ip
 * @property string $controller
 * @property string $action
 * @property int $objId
 * @property string $result
 */
class Adminlog extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tb_adminlog';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['addtime', 'admin_ip', 'objId','admin_id'], 'integer'],
            [['title', 'result'], 'string', 'max' => 255],
            [['admin_name', 'controller', 'action'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'log_id' => '操作记录ID',
            'title' => '操作记录描述',
            'addtime' => '记录时间',
            'admin_name' => '操作人姓名',
            'admin_id' => '操作人ID',
            'admin_ip' => '操作人IP地址',
            'controller' => '操作控制器名称',
            'action' => '操作类型',
            'objId' => '操作数据编号',
            'result' => '操作结果',
        ];
    }
    
    public static function saveLog($controller ,$action,$result,$objId){
        $model = new self;
        $model->admin_ip = Yii::$app->request->userIP;
      //  $headers = Yii::$app->request->headers;
        $model->addtime = time();
        /* if ($headers->has('User-Agent')) {
            $model->admin_agent =  $headers->get('User-Agent');
        } */
        $model->admin_id = Yii::$app->user->identity->id;
        $model->admin_name = Yii::$app->user->identity->email;
    
        /* $controllers = ['article','video','collection','collection-album','category','banner','exchange','user','admin'];
        if(!in_array(strtolower($controller),$controllers)) $controller = '';
        $actions = ['create','update','delete','login','logout'];
        if(!in_array(strtolower($action),$actions))$action = '';
     */
        $model->controller = $controller;
        $model->action = $action;
        $model->result = $result;
        $model->objId = $objId;
        $model->title =  $model->admin_name.' '.$model->action.' '.$model->controller;
        $model->save(false);
    
    }
    
}
